import {
	createRouter,
	createWebHashHistory
} from "vue-router"
import Layout from '@/layout/main.vue'

const routes = [{
	path: "/",
	component: Layout,
	children: [
		{
			path:"/",
			name:"home",
			component:()=>import("@/pages/Home.vue"),
			meta:{
				title:"领导驾驶舱"
			}
		}
	]
},
{
	path:"/login",
	name:"login",
	component:()=>import("@/pages/Login.vue"),
	meta:{
		title:"登录"
	}
}
]

//添加到创建的路由中
const router = createRouter({
	history: createWebHashHistory(),
	routes
})

const whiteList = ['/login', '/familyInfo'] 

router.beforeEach((to, from, next) => {
	const hasToken = localStorage.SDRS_TOKEN
	if (hasToken) {
		if (to.path === '/login') {
			next({ path: '/' })
		}else {
			next()
		}
	} else {
		if (whiteList.indexOf(to.path) !== -1) {
			// 在免登录白名单，直接进入
			next()
		} else {
			// other pages that do not have permission to access are redirected to the login page.
			next({ path: '/login' })
		}
	}
});
export default router
